/** common.js By Beginner Emain:zheng_jinfan@126.com HomePage:http://www.zhengjinfan.cn */
layui.define(['layer'], function(exports) {
	"use strict";

	var $ = layui.jquery,
		layer = layui.layer;

	var common = {
		/**
		 * 抛出一个异常错误信息
		 * @param {String} msg
		 */
		throwError: function(msg) {
			throw new Error(msg);
			return;
		},
		/**
		 * 弹出一个错误提示
		 * @param {String} msg
		 */
		msgError: function(msg) {
			layer.msg(msg, {
				icon: 5
			});
			return;
		},
		/**
		 * 弹框
		 */
		layerOpen : function(obj,callback) {
			if (!obj) obj = {};
			layer.open({
				type: obj.type || 1,
				id: 'LAY_layuipro', //设定一个id，防止重复弹出
				title: obj.title || '',
				content: obj.content || '',
				skin: obj.skin || 'layui-layer-molv', //自定义样式
				area: obj.area || ['400px', '500px'],
				maxmin: false || obj.maxmin,
				resize: false,
				btn: obj.btn || [],
				btnAlign: obj.btnAlign || 'r',
				success: obj.success || function() {
					
				},
				yes: callback || function(index, layero){
				    layer.close(index); //如果设定了yes回调，需进行手工关闭
				},
				cancel:function(index, layero){
					/*obj.content.hide();*/
				    layer.close(index); //如果设定了yes回调，需进行手工关闭
				},
			});
		},
		/**
		 * 确认提示
		 */
		layerConfirm : function(text, callback) {
			layer.confirm(text, {
				id : 'CE_deletepro',
				icon : 3,
				title : '温馨提示',
				skin : 'layui-layer-molv',
				resize: false,
				anim : Math.ceil(Math.random() * 6)	
			}, callback || function() {

			});
		},
		/* 图片 */
		convertImgToBase64 :	function (url, callback, outputFormat){
		   var canvas = document.createElement('CANVAS'),
		　　ctx = canvas.getContext('2d'),
		　　img = new Image;
		　　img.crossOrigin = 'Anonymous';
		　　img.onload = function(){
			　　canvas.height = img.height;
			　　canvas.width = img.width;
			　　ctx.drawImage(img,0,0);
			　　var dataURL = canvas.toDataURL(outputFormat || 'image/png');
			　　callback.call(this, dataURL);
			　　canvas = null; 
			};
		　　img.src = url;
		}    
	};

	exports('common', common);
});